﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace List
{
	class Program
	{
		static Dictionary<string, Dictionary<string, string>> PhoneBook =new Dictionary<string, Dictionary<string, string>>();

		static void Main(string[] args)
		{
			string selection = Menu0();

			while (true)
			{
				
				switch (selection)
				{
					case "1":
						selection = Menu1();
						break;
					case "2":
						selection = Menu2();
						break;
					case "3":
						selection = Menu3();
						break;
					case "4":
						selection = Menu4();
						break;
					case "0":
						return;
					case "":
						selection = Menu0();
						break;
					default:
						Console.WriteLine("Wrong input");
						selection = Menu0();
						break;
				}
			}


			//List<int> numbers = new List<int>(){ 2, 3, 4, 5, 6, 5};
			//List<int> numbers2 = new List<int>() { 22, 23, 224, 5, 46, 35 };
			//numbers.AddRange(numbers2);
			//Console.WriteLine(numbers);
			//foreach(var i in numbers)
			//{
			//	Console.WriteLine(i);
			//}

			//Dictionary<int, string> d = new Dictionary<int, string>();
			//d[1] = "odin";
			//d[2] = "dva";
			//d[3] = "tri";
			//d[123] = "odin dva tri";
			//d[0] = "null";
			//Console.WriteLine("=====================================");
			//foreach (var entry in d)
			//{
			//	Console.WriteLine("{0} - {1}", entry.Key, entry.Value);
			//}
			//Console.WriteLine("=====================================");
			//foreach (var key in d.Keys)
			//{
			//	Console.WriteLine("{0} - {1}", key, d[key]);
			//}
			//Console.WriteLine("=====================================");
			//foreach (var v in d.Values)
			//{
			//	Console.WriteLine("{0} - {1}", "???", v);
			//}

			Console.ReadKey();


		}

		static string Menu0()
		{
			Console.WriteLine("1. Add city");
			Console.WriteLine("2. Add number");
			Console.WriteLine("3. Search number");
			Console.WriteLine("4. Show city numbers");

			string v = Console.ReadLine();
			Console.Clear();

			return v;
		}

		static string Menu1()
		{
			Console.Write("Enter city name: ");
			string city = Console.ReadLine();
			if (PhoneBook.ContainsKey(city))
			{
				Console.WriteLine("City already exists");
			}
			else
			{
				PhoneBook.Add(city, new Dictionary<string, string>());
				Console.WriteLine("City has been added");
			}

			return string.Empty;
		}

		static string Menu2()
		{
			Console.WriteLine("Choose any of the existing Cities ");
			for (int i = 0; i < PhoneBook.Keys.Count; i++)
			{
				Console.WriteLine("{0}.{1}", i+1, PhoneBook.Keys.ElementAt(i));
			}

			int cityNumber;
			if (!int.TryParse(Console.ReadLine(), out cityNumber))
			{
				Console.WriteLine("Input is not a number");
			}
			else if(cityNumber > 0 && cityNumber <= PhoneBook.Keys.Count)
			{
				string city = PhoneBook.Keys.ElementAt(cityNumber - 1);

				Console.WriteLine("Please enter phone number to add");
				string phoneNumber = Console.ReadLine();

				Console.WriteLine("Please enter first name and last name to add");
				string name = Console.ReadLine();

				Dictionary<string, string> cityPhoneBook = PhoneBook[city];
				cityPhoneBook[phoneNumber] = name;
			}
			else
			{
				Console.WriteLine("No such city");
			}

			return string.Empty;
		}

		static string Menu3()
		{
			Console.WriteLine("Please enter phone number");
			string phonenumber = Console.ReadLine();
			foreach (string city in PhoneBook.Keys)
			{
				Dictionary<string, string> cityPhoneBook = PhoneBook[city];
				if (!cityPhoneBook.ContainsKey(phonenumber))
				{
					Console.WriteLine("Number doesnt exist in " + city);
				}
				else
				{
					Console.WriteLine("{0}  {1}", city, cityPhoneBook[phonenumber]);
				}
				
            }

			return string.Empty;

		}

		static string Menu4()
		{
			foreach (string city in PhoneBook.Keys)
			{
				//Console.WriteLine(PhoneBook);
				foreach (string phone in PhoneBook[city].Keys)
				{
					Console.WriteLine("{0} {1}", phone, PhoneBook[city][phone]);
                }
			}
				return string.Empty;
		}

	}
}
